
    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_Subscriptions_Subscriber]') AND parent_object_id = OBJECT_ID('AF_SUBSCRIPTIONS'))
alter table AF_SUBSCRIPTIONS  drop constraint FK_Subscriptions_Subscriber

    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_Topics_Update]') AND parent_object_id = OBJECT_ID('AF_TOPICS'))
alter table AF_TOPICS  drop constraint FK_Topics_Update

    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_Comments_Update]') AND parent_object_id = OBJECT_ID('AF_COMMENTS'))
alter table AF_COMMENTS  drop constraint FK_Comments_Update

    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_UserGroups_Update]') AND parent_object_id = OBJECT_ID('AF_USERGROUPS'))
alter table AF_USERGROUPS  drop constraint FK_UserGroups_Update

    if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_Likes_Update]') AND parent_object_id = OBJECT_ID('AF_LIKES'))
alter table AF_LIKES  drop constraint FK_Likes_Update

    if exists (select * from dbo.sysobjects where id = object_id(N'AF_SUBSCRIBER') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_SUBSCRIBER
    if exists (select * from dbo.sysobjects where id = object_id(N'AF_SUBSCRIPTIONS') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_SUBSCRIPTIONS
    if exists (select * from dbo.sysobjects where id = object_id(N'AF_UPDATE') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_UPDATE
    if exists (select * from dbo.sysobjects where id = object_id(N'AF_TOPICS') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_TOPICS
    if exists (select * from dbo.sysobjects where id = object_id(N'AF_COMMENTS') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_COMMENTS
    if exists (select * from dbo.sysobjects where id = object_id(N'AF_USERGROUPS') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_USERGROUPS
    if exists (select * from dbo.sysobjects where id = object_id(N'AF_LIKES') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table AF_LIKES
    if exists (select * from dbo.sysobjects where id = object_id(N'hibernate_unique_key') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table hibernate_unique_key
    create table AF_SUBSCRIBER (
        Id NVARCHAR(255) not null,
       primary key (Id)
    )
    create table AF_SUBSCRIPTIONS (
        SubscriberId NVARCHAR(255) not null,
       Id NVARCHAR(255) null
    )
    create table AF_UPDATE (
        Id NVARCHAR(255) not null,
       PublishedBy NVARCHAR(255) null,
       PublishedOn DATETIME null,
       Source NVARCHAR(255) null,
       IsPublic BIT null,
       Content NVARCHAR(MAX) null,
       primary key (Id)
    )
    create table AF_TOPICS (
        UpdateId NVARCHAR(255) not null,
       Id NVARCHAR(255) null
    )
    create table AF_COMMENTS (
        UpdateId NVARCHAR(255) not null,
       PublishedBy NVARCHAR(255) null,
       PublishedOn DATETIME null,
       Text NVARCHAR(MAX) null
    )
    create table AF_USERGROUPS (
        UpdateId NVARCHAR(255) not null,
       Id NVARCHAR(255) null
    )
    create table AF_LIKES (
        UpdateId NVARCHAR(255) not null,
       UserId NVARCHAR(255) null,
       CreatedDate DATETIME null
    )
    alter table AF_SUBSCRIPTIONS 
        add constraint FK_Subscriptions_Subscriber 
        foreign key (SubscriberId) 
        references AF_SUBSCRIBER
    alter table AF_TOPICS 
        add constraint FK_Topics_Update 
        foreign key (UpdateId) 
        references AF_UPDATE
    alter table AF_COMMENTS 
        add constraint FK_Comments_Update 
        foreign key (UpdateId) 
        references AF_UPDATE
    alter table AF_USERGROUPS 
        add constraint FK_UserGroups_Update 
        foreign key (UpdateId) 
        references AF_UPDATE
    alter table AF_LIKES 
        add constraint FK_Likes_Update 
        foreign key (UpdateId) 
        references AF_UPDATE
    create table hibernate_unique_key (
         next_hi INT 
    )
    insert into hibernate_unique_key values ( 1 )
    IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'IX_Topics_Id') CREATE INDEX IX_Topics_Id ON AF_TOPICS (Id) INCLUDE (UpdateId)
    IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'IX_Subscriptions') CREATE INDEX IX_Subscriptions ON AF_SUBSCRIPTIONS (Id) INCLUDE (SubscriberId)
    IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'IX_Update_PublishedOn') CREATE INDEX IX_Update_PublishedOn ON AF_UPDATE (PublishedOn) INCLUDE (Id)
    ALTER TABLE AF_TOPICS DROP CONSTRAINT FK_Topics_Update
    ALTER TABLE AF_TOPICS ADD CONSTRAINT FK_Topics_Update FOREIGN KEY (UpdateId) REFERENCES AF_UPDATE ON DELETE CASCADE
    ALTER TABLE AF_COMMENTS DROP CONSTRAINT FK_Comments_Update
    ALTER TABLE AF_COMMENTS ADD CONSTRAINT FK_Comments_Update FOREIGN KEY (UpdateId) REFERENCES AF_UPDATE ON DELETE CASCADE
    ALTER TABLE AF_USERGROUPS DROP CONSTRAINT FK_UserGroups_Update
    ALTER TABLE AF_USERGROUPS ADD CONSTRAINT FK_UserGroups_Update FOREIGN KEY (UpdateId) REFERENCES AF_UPDATE ON DELETE CASCADE
    ALTER TABLE AF_LIKES DROP CONSTRAINT FK_Likes_Update
    ALTER TABLE AF_LIKES ADD CONSTRAINT FK_Likes_Update FOREIGN KEY (UpdateId) REFERENCES AF_UPDATE ON DELETE CASCADE